home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / expdict.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  2KB  |  98 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: expdict.py 600 2005-08-19 10:45:03Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import time
  7. import threading
  8.  
  9. class ExpiringDictionary(dict):
  10.     __slots__ = [
  11.         '_timeouts',
  12.         '_default_timeout',
  13.         '_lock']
  14.     
  15.     def __init__(self, default_timeout = 300):
  16.         dict.__init__(self)
  17.         self._timeouts = { }
  18.         self._default_timeout = default_timeout
  19.         self._lock = threading.RLock()
  20.  
  21.     
  22.     def __delitem__(self, key):
  23.         self._lock.acquire()
  24.         
  25.         try:
  26.             del self._timeouts[key]
  27.             return dict.__delitem__(self, key)
  28.         finally:
  29.             self._lock.release()
  30.  
  31.  
  32.     
  33.     def __getitem__(self, key):
  34.         self._lock.acquire()
  35.         
  36.         try:
  37.             self._expire_item(key)
  38.             return dict.__getitem__(self, key)
  39.         finally:
  40.             self._lock.release()
  41.  
  42.  
  43.     
  44.     def __setitem__(self, key, value):
  45.         return self.set_item(key, value)
  46.  
  47.     
  48.     def set_item(self, key, value, timeout = None, timeout_callback = None):
  49.         self._lock.acquire()
  50.         
  51.         try:
  52.             if not timeout:
  53.                 timeout = self._default_timeout
  54.             
  55.             self._timeouts[key] = (time.time() + timeout, timeout_callback)
  56.             return dict.__setitem__(self, key, value)
  57.         finally:
  58.             self._lock.release()
  59.  
  60.  
  61.     
  62.     def expire(self):
  63.         self._lock.acquire()
  64.         
  65.         try:
  66.             for k in self._timeouts.keys():
  67.                 self._expire_item(k)
  68.         finally:
  69.             self._lock.release()
  70.  
  71.  
  72.     
  73.     def _expire_item(self, key):
  74.         (timeout, callback) = self._timeouts[key]
  75.         if timeout <= time.time():
  76.             del self[key]
  77.             if callback:
  78.                 
  79.                 try:
  80.                     callback(key, dict.__getitem__(self, key))
  81.                 except TypeError:
  82.                     
  83.                     try:
  84.                         callback(key)
  85.                     except TypeError:
  86.                         callback()
  87.                     except:
  88.                         None<EXCEPTION MATCH>TypeError
  89.                     
  90.  
  91.                     None<EXCEPTION MATCH>TypeError
  92.                 
  93.  
  94.             None<EXCEPTION MATCH>TypeError
  95.         
  96.  
  97.  
  98.